// var FactoryItem = require("FactoryItem");

var FlyUI = cc.Class({
  extends: cc.Component,

  statics: {
    Instance: null,
  },

  properties: {
    FlyScoreUIPrefabs: cc.Node,
    _FlyScoreUIList: [],

    RemianFlyUIPrefab: cc.Node,
    _RemianFlyUIList: [],

    FlyPropsUIPrefabs: cc.Node,
    _FlyPropsUIList: [],

    PropsUISpriteFrame: {
      type: cc.SpriteFrame,
      default: [],
    },
    //UIMian:cc.Node,
  },

  onLoad() {
    FlyUI.Instance = this;
  },

  start() {
    this.UIMian = null;
  },
  /**
   * ，用于创建分数飞行动画
   * @param {*} m_pos 起始位置
   * @param {*} _node 目标节点
   * @param {*} score 分数
   */
  CreatorScoreFly(m_pos, _node, score) {
    //   转换目标节点的世界坐标
    var newVec2 = _node.convertToWorldSpaceAR(cc.v2(0, 0));
    // 检查 _FlyScoreUIList 是否有可用的飞行动画节点，如果有则复用，否则创建一个新的节点
    var FlyScore =
      this._FlyScoreUIList.length > 0
        ? this._FlyScoreUIList.pop()
        : cc.instantiate(this.FlyScoreUIPrefabs);
    // 将飞行动画节点的位置设置为 m_pos
    FlyScore.setPosition(m_pos);
    // 将飞行动画节点的缩放设置为 0，使其不可见
    FlyScore.setScale(0);
    // 飞行动画节点的父节点设置为当前节点，确保其在正确的层级中显示
    FlyScore.parent = this.node;
    // 获取 FlyUIItem 组件并设置分数
    FlyScore.getComponent("FlyUIItem").setScore(score);
    FlyScore.active = true;
    // 定义飞行动画的动作序列
    var s = cc.scaleTo(0.3, 1.5);

    var s1 = cc.scaleTo(0.1, 1);

    var wait = cc.scaleTo(0.2, 1.2);

    var m = cc.moveTo(1, newVec2);

    var call = cc.callFunc(
      function () {
        // 将飞行动画节点设为不可见
        FlyScore.active = false;
        // 更新得分显示
        this.UIMian.getComponent("UIMian").setLableUI(score);
        // 将飞行动画节点放回 _FlyScoreUIList 以便复用
        this._FlyScoreUIList.push(FlyScore);
      }.bind(this)
    );

    FlyScore.runAction(cc.sequence(s, s1, wait, m, call));
  },

  PropsUIFly(index) {
    var PropsFly =
      this._FlyPropsUIList.length > 0
        ? this._FlyPropsUIList.pop()
        : cc.instantiate(this.FlyPropsUIPrefabs);

    PropsFly.setPosition(cc.find("Canvas").getPosition());

    PropsFly.setScale(0.1);

    (PropsFly.parent = this.node),
      (PropsFly.getComponent(cc.Sprite).spriteFrame =
        this.PropsUISpriteFrame[index]);

    if (index == 0) {
      PropsFly.getChildByName("Count").getComponent(cc.Label).string = "x5";
    } else {
      PropsFly.getChildByName("Count").getComponent(cc.Label).string = "x1";
    }

    PropsFly.active = true;

    var targetNode = null;

    var action1 = null;

    var UIMianCom = this.UIMian.getComponent("UIMian");

    switch (index) {
      case 0:
        targetNode = UIMianCom.DiamondCountLabel.node;
        action1 = () => {
          UIMianCom.UserDiamond(5);
        };
        break;
      case 1:
        targetNode = UIMianCom.PropsRest;
        action1 = () => {
          UIMianCom.UserReset(1);
        };
        break;
      case 2:
        targetNode = UIMianCom.PropsHammer;
        action1 = () => {
          UIMianCom.UserHammerProps(1);
        };
        break;
      case 3:
        targetNode = UIMianCom.PropsChange;
        action1 = () => {
          UIMianCom.UserChange(1);
        };
        break;
    }
    var newVec2 = targetNode.convertToWorldSpaceAR(cc.v2(0, 0));

    var s = cc.scaleTo(0.2, 1);

    var m = cc.moveTo(0.6, newVec2);

    var call = cc.callFunc(
      function () {
        PropsFly.active = false;

        action1();

        this._FlyPropsUIList.push(PropsFly);
      }.bind(this)
    );

    PropsFly.runAction(cc.sequence(s, m, call));
  },

  RemianFlyUI(sartnode, end_node, score, action) {
    var newVec1 = sartnode.convertToWorldSpaceAR(cc.v2(0, 0));

    var newVec2 = end_node.convertToWorldSpaceAR(cc.v2(0, 0));

    var FlyScore =
      this._RemianFlyUIList.length > 0
        ? this._RemianFlyUIList.pop()
        : cc.instantiate(this.RemianFlyUIPrefab);

    FlyScore.setPosition(newVec1);

    FlyScore.setScale(0.1);

    FlyScore.parent = this.node;

    FlyScore.getComponent(cc.Label).string = score;

    FlyScore.active = true;

    var s = cc.scaleTo(0.2, 1);

    var m = cc.moveTo(0.5, newVec2);

    var call = cc.callFunc(
      function () {
        FlyScore.active = false;

        this.UIMian.getComponent("UIMian").setLableUI(score);

        this._RemianFlyUIList.push(FlyScore);

        action();
      }.bind(this)
    );

    if (score > 0) {
      FlyScore.runAction(cc.sequence(s, m, call));
    } else {
      FlyScore.active = false;
      this._RemianFlyUIList.push(FlyScore);
      action();
    }
  },
});
